var t = require("../../../@babel/runtime/helpers/objectSpread2"),
  e = require("../../../@babel/runtime/helpers/regeneratorRuntime"),
  a = require("../../../@babel/runtime/helpers/asyncToGenerator"),
  i = require("../../../@babel/runtime/helpers/slicedToArray"),
  o = require("../../../utils/helper/map/index"),
  n = require("../../../utils/index"),
  c = require("../../../utils/tools/common/index"),
  s = n.helper.location;
Page({
  useStore: function (t) {
    return {
      userLocation: t.storage.userLocation
    }
  },
  data: {
    latitude: Number,
    longitude: Number,
    address: "",
    pois: [],
    markers: [{
      iconPath: "https://statics.zhaogongdi.com/images/wp/location.png",
      latitude: "",
      longitude: "",
      id: 1,
      width: 40,
      height: 40
    }],
    location: "",
    selectCity: "",
    cityLabel: "",
    cityCode: "",
    cityData: [],
    selectLevel: 2,
    pickValue: [],
    showCityPicker: !1,
    showMap: !0,
    paddingTop: (0, c.getHeaderHeight)("8rpx")
  },
  setLocation: function (t) {
    var e = this;
    (0, o.getLocation)(t).then((function (t) {
      if (!t.errMsg) {
        var a = t.address,
          n = t.latitude,
          c = t.longitude,
          s = t.pois,
          r = t.name,
          l = t.adcode;
        if (s.length) {
          var u = e.data.userLocation;
          s.forEach((function (t) {
            if (u) {
              var e = t.location.split(","),
                a = i(e, 2),
                n = a[0],
                c = a[1],
                s = u.split(","),
                r = i(s, 2),
                l = r[0],
                d = r[1];
              t.trueDistance = (0, o.getDistance)(+c, +n, +d, +l)
            }
          }));
          var d = (0, o.codeSearchCity)(l),
            h = d.params.city_text,
            p = d.city;
          e.setData({
            name: r,
            address: a,
            latitude: n,
            longitude: c,
            cityLabel: h,
            pois: s,
            cityCode: l || "0",
            "markers[0].latitude": n,
            "markers[0].longitude": c,
            pickValue: [p]
          }), e.selectComponent("#cityPicker").setDefaultActive()
        } else $.jxAlert({
          content: "地址不合法，请重新选择"
        })
      }
    })).catch((function (t) {
      console.log("getLocation.catch :>> ", t), 2 == t.errCode && $.msg("请打开手机定位")
    })).finally((function () {
      wx.hideLoading()
    }))
  },
  onMapClick: function (t) {
    var i = this;
    return a(e().mark((function a() {
      var o, n, c, s;
      return e().wrap((function (e) {
        for (;;) switch (e.prev = e.next) {
          case 0:
            o = t.detail, n = o.latitude, c = o.longitude, s = "".concat(c, ",").concat(n), i.setData({
              longitude: c,
              latitude: n,
              "markers[0].latitude": n,
              "markers[0].longitude": c,
              location: s
            }), wx.showLoading({
              title: "定位中"
            }), i.setLocation({
              location: s
            });
          case 3:
          case "end":
            return e.stop()
        }
      }), a)
    })))()
  },
  onPickAddress: function (e) {
    var a = e.detail,
      n = a.name,
      c = a.location.split(","),
      s = i(c, 2),
      r = s[0],
      l = s[1],
      u = t({
        address: n,
        longitude: r,
        latitude: l
      }, (0, o.codeSearchCity)(this.data.cityCode).params);
    $.router.back({
      params: t(t({}, u), {}, {
        "page-type": "location"
      })
    })
  },
  onSelectCity: function () {
    $.router.push("/subpackage/common/multi-role-select/index", {
      type: "city",
      onlyCity: 1,
      isUpdateStorage: 0,
      selectValue: JSON.stringify(this.data.pickValue)
    })
  },
  onClose: function () {
    this.setData({
      showCityPicker: !1
    })
  },
  onCityChange: function (t) {
    var e = this,
      a = t.detail.pick;
    console.log("pick :>> ", a);
    var n = a.value[0].adcode,
      c = this.data.userLocation;
    this.setData({
      selectCity: a,
      cityLabel: a.label,
      pickValue: a.value
    }), (0, o.getSearchLocation)({
      city: n,
      keywords: a.label
    }).then((function (t) {
      var a = t;
      a.forEach((function (t) {
        if (t.address = t.district, c) {
          var e = t.location.split(","),
            a = i(e, 2),
            n = a[0],
            s = a[1],
            r = c.split(","),
            l = i(r, 2),
            u = l[0],
            d = l[1];
          t.trueDistance = (0, o.getDistance)(+s, +n, +d, +u)
        }
      }));
      var s = a[0].location;
      e.setData({
        pois: a,
        cityCode: n
      });
      var r = s.split(","),
        l = i(r, 2),
        u = l[0],
        d = l[1];
      e.setData({
        latitude: d,
        longitude: u,
        "markers[0].latitude": d,
        "markers[0].longitude": u
      })
    })), this.onClose()
  },
  onFocus: function () {
    this.setData({
      showMap: !1
    })
  },
  onInput: (0, c.debounce)((function (t) {
    var e = this,
      a = t.detail.value,
      n = this.data.cityCode,
      c = this.data.userLocation;
    (0, o.getSearchLocation)({
      keywords: a,
      city: n
    }).then((function (t) {
      var a = t;
      a.forEach((function (t) {
        if (t.address = t.district, c) {
          var e = t.location.split(","),
            a = i(e, 2),
            n = a[0],
            s = a[1],
            r = c.split(","),
            l = i(r, 2),
            u = l[0],
            d = l[1];
          t.trueDistance = (0, o.getDistance)(+s, +n, +d, +u)
        }
      })), e.setData({
        pois: a
      })
    }))
  }), 500),
  customBack: function () {
    this.data.showMap ? $.router.back() : this.setData({
      showMap: !0
    })
  },
  onCustomBack: function () {
    this.customBack()
  },
  getCurrentLocation: function () {
    this.setLocation(), this.setData({
      pickValue: [],
      selectCity: ""
    })
  },
  onLoad: function (t) {
    var i = this;
    return a(e().mark((function a() {
      var o, n, c, r;
      return e().wrap((function (e) {
        for (;;) switch (e.prev = e.next) {
          case 0:
            return o = t.longitude, n = t.latitude, e.next = 4, s.getAreaData();
          case 4:
            c = e.sent, i.setData({
              cityData: c
            }), o && n ? (r = "".concat(o, ",").concat(n), i.setLocation({
              location: r
            })) : i.setLocation();
          case 6:
          case "end":
            return e.stop()
        }
      }), a)
    })))()
  },
  onShow: function () {
    var t = this,
      e = $.router.getParams();
    if ("city" == (null == e ? void 0 : e["select-type"]) && null != e && e.label && null != e && e.value) {
      var a = e,
        n = a.value[0].adcode,
        c = this.data.userLocation;
      this.setData({
        selectCity: a,
        cityLabel: a.label,
        pickValue: a.value
      }), (0, o.getSearchLocation)({
        city: n,
        keywords: a.label
      }).then((function (e) {
        var a = e;
        a.forEach((function (t) {
          if (t.address = t.district, c) {
            var e = t.location.split(","),
              a = i(e, 2),
              n = a[0],
              s = a[1],
              r = c.split(","),
              l = i(r, 2),
              u = l[0],
              d = l[1];
            t.trueDistance = (0, o.getDistance)(+s, +n, +d, +u)
          }
        }));
        var s = a[0].location;
        t.setData({
          pois: a,
          cityCode: n
        });
        var r = s.split(","),
          l = i(r, 2),
          u = l[0],
          d = l[1];
        t.setData({
          latitude: d,
          longitude: u,
          "markers[0].latitude": d,
          "markers[0].longitude": u
        })
      }))
    }
  }
});